package dao;

import entity.Flower;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 花卉数据访问对象实现类
 */
public class FlowerDaoImpl extends BaseDao implements FlowerDao {

    @Override
    public List<Flower> getAllFlowers() {
        List<Flower> flowers = new ArrayList<>();
        String sql = "SELECT * FROM flower";
        ResultSet rs = executeQuery(sql);

        try {
            while (rs.next()) {
                Flower flower = new Flower();
                flower.setId(rs.getInt("id"));
                flower.setName(rs.getString("name"));
                flower.setAnothername(rs.getString("anothername"));
                flower.setProperty(rs.getString("property"));
                flower.setPrice(rs.getFloat("price"));
                flower.setProduction(rs.getString("production"));
                flowers.add(flower);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection, pstmt, rs);
        }
        return flowers;
    }

    @Override
    public boolean addFlower(Flower flower) {
        String sql = "INSERT INTO flower (name, anothername, property, price, production) VALUES (?, ?, ?, ?, ?)";
        Object[] params = {flower.getName(), flower.getAnothername(), flower.getProperty(), flower.getPrice(), flower.getProduction()};
        int result = executeUpdate(sql, params);
        return result > 0;
    }
}
